﻿/*************************************************************************
 **项目名称： MPACorePhoneBook.PhoneBooks   
 **项目描述：      
 **类 名 称： PersonAppService
 **说    明： 
 **作    者： 马忠磊
 **邮    箱： 524402195@qq.com
 **微    信： mazhonglei070246
 **博    客： https://blog.csdn.net/mzl87
 **创建时间： 2019/5/5 16:38:32
 **修 改 人：        
 **修改时间：    
**************************************************************************/
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.UI;
using Microsoft.EntityFrameworkCore;
using MPACorePhoneBook.PhoneBooks.Dto;
using MPACorePhoneBook.PhoneBooks.Persons;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace MPACorePhoneBook.PhoneBooks
{
    /// <summary>
    /// PersonAppService 的摘要说明
    /// </summary>
    public class PersonAppService : MPACorePhoneBookAppServiceBase, IPersonAppService
    {
        private readonly IRepository<Person> _personRepository;

        public PersonAppService(IRepository<Person> personRepository)
        {
            _personRepository = personRepository;
        }

        public async Task CreateOrUpdatePersonAsync(CreateOrUpdatePersonInput input)
        {
            if (input.PersonEditDto.Id.HasValue)
            {
                await UpdatePersonAsync(input.PersonEditDto);
            }
            else
            {
                await CreatePersonAsync(input.PersonEditDto);
            }

        }

        public async Task DeletePersonAsync(EntityDto input)
        {
            var entity = await this._personRepository.GetAsync(input.Id);
            if (entity == null)
            {
                throw new UserFriendlyException("该联系人已经消失在数据库中，无法二次删除");
            }

            await this._personRepository.DeleteAsync(input.Id);
        }

        public async Task<PagedResultDto<PersonListDto>> GetPagedPersonAsync(GetPersonInput input)
        {
            var query = this._personRepository.GetAllIncluding(a=>a.PhoneNumbers);

            var personCount = await query.CountAsync();

            var persons = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            var dtos = persons.MapTo<List<PersonListDto>>();

            return new PagedResultDto<PersonListDto>(personCount, dtos);
        }

        public async Task<PersonListDto> GetPersonByIdAsync(NullableIdDto input)
        {
            var person = await this._personRepository.GetAllIncluding(a=>a.PhoneNumbers).FirstOrDefaultAsync(x=>x.Id == input.Id.Value);

            return person.MapTo<PersonListDto>();
        }


        protected async Task UpdatePersonAsync(PersonEditDto input)
        {
            var entity = await _personRepository.GetAsync(input.Id.Value);

            await _personRepository.UpdateAsync(input.MapTo(entity));
        }

        protected async Task CreatePersonAsync(PersonEditDto input)
        {
            var entity = input.MapTo<Person>();
            await this._personRepository.InsertAsync(entity);
        }

        public async Task<GetPersonForEditOutput> GetPersonForEditAsync(NullableIdDto input)
        {
            var output = new GetPersonForEditOutput();
            PersonEditDto personEditDto;

            if (input.Id.HasValue)
            {
                var entity = await this._personRepository.GetAllIncluding(a => a.PhoneNumbers).FirstOrDefaultAsync(x => x.Id == input.Id.Value);
                personEditDto = entity.MapTo<PersonEditDto>();
            }
            else
            {
                personEditDto = new PersonEditDto();
            }

            output.Person = personEditDto;
            return output;
        }
    }
}
